package com.jwell.panrank.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwell.panrank.R;


/**
 * 车牌选择器
 */

public class LicenseChooseAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private String[] mCities;
    private String[] mLicenseNum;

    private static final int TYPE_CITIES = 0;
    private static final int TYPE_LICENSENUM = 1;

    public int getPlateType() {
        return plateType;
    }

    public void setPlateType(int plateType) {
        this.plateType = plateType;
    }

    private int plateType;

    public LicenseChooseAdapter(Context context, String[] cities, String[] licenseNum) {
        mContext = context;
        mCities = cities;
        mLicenseNum = licenseNum;
    }

    public interface OnLicenseClickListener {
        void onItemClick(View view, int position);
    }

    //定义一个监听对象，用来储存监听事件
    private OnLicenseClickListener mListener;

    public void setOnLicenseCliskListener(OnLicenseClickListener listener) {
        mListener = listener;
    }

    public class LicenseChooseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public TextView mItemView;
        public RelativeLayout mRelativeLayout;

        public LicenseChooseViewHolder(View itemView) {
            super(itemView);
            mItemView = (TextView) itemView.findViewById(R.id.tv_license_item);
            mRelativeLayout = (RelativeLayout) itemView;
            mRelativeLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mListener!=null){
                mListener.onItemClick(v,getPosition());
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LicenseChooseViewHolder viewHolder = new LicenseChooseViewHolder(LayoutInflater.from(mContext).inflate(R.layout.license_item,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LicenseChooseViewHolder viewHolder = (LicenseChooseViewHolder) holder;
        viewHolder.mItemView.setGravity(Gravity.CENTER);
        if (mCities!=null){
            viewHolder.mItemView.setText(mCities[position]);
        }
        if (mLicenseNum!=null){
            viewHolder.mItemView.setText(mLicenseNum[position]);
        }

    }

    @Override
    public int getItemCount() {
        if (mCities!=null){
            return mCities.length;
        }
        if (mLicenseNum!=null){
            return mLicenseNum.length;
        }
        return 0;
    }
}
